//
//  XLMenuBar.swift
//  soapp
//
//  Created by zhu xietong on 2017/8/30.
//  Copyright © 2017年 CocoaPods. All rights reserved.
//

import Foundation
import Eelay


open class XLMenuBar:JoView,EeSegBarDelegate{
    
    public var selectedColor = UIColor._tint
    public var normalColor = UIColor(shex: "#888")
    open func observer(progress: CGFloat, currentIndex: Int, currentScale: CGFloat, nearIndex: Int, nearScale: CGFloat) {
        
    }
    
    open func touched(oldIndex: Int, to: Int) {
        
    }
    
    open func selected(index: Int) {
        var name = ""
        if index < bar.names.count{
            name = bar.names[index]
        }
        bar.index = index
        self.selectedMenu(index, name)
    }
    
    public let bar = EeSegBar()
    
    open var selectedMenu:(_ Index:Int,String) ->Void = {_,_ in}
    
    required public init() {
        super.init(frame: [0,0,Swidth,40])
    }
    
    required public init(frame: CGRect) {
        super.init(frame:frame)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    let scrollV = UIScrollView()
    public var pageIndex = 0
    {
        willSet{
            if newValue != pageIndex
            {
                bar.index = newValue
            }
        }
    }
    
    open func loadItems(items:[String],width:CGFloat=Swidth - 32) {
        bar.names = items
        bar.index = 0
        bar.delegate = self
//        let width = Swidth - 32
        var ui = EeSegBarStyle()
        ui.indicator_color = selectedColor
        ui.normal_color = UIColor(shex: "#888")
        ui.select_color = selectedColor
        ui.seg_width = width/items.count.cg_floatValue
//        ui.indicator_width =  width/items.count.cg_floatValue
        
        ui.indicator_width = 50
        bar.ui = ui
        bar.frame = [16,0,width,self.frame.height]
        bar.loadItems()

    }
    
    public let line = UIView()
    open override func addLayoutRules() {
//        let width = Swidth - 32
        
        self.addSubview(bar)
        self.eelay = [
            [bar,[ee.T.L.B.R,[0,16,-0,-16]]],
            [line,[ee.L.B.R],"\(TP.separatorHeight)"]
        ]
        line.backgroundColor = ._separator
    }
    
    open override var intrinsicContentSize: CGSize{
        get{
            return self.frame.size
        }
    }
    
}

